<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="600" append-to-body destroy-on-close @closed="$emit('closed')">
		<el-container>
			<el-main class="nopadding">
				<el-scrollbar>
					<el-row class="drawer-table">
						<el-col :span="24">
							<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="80px">
								<el-form-item label="上级栏目" prop="parentId">
									<el-cascader v-model="form.parentId" :options="superCategoryOptions" :props="superCategoryProps"
										:show-all-levels="false" placeholder="上级栏目"></el-cascader>
								</el-form-item>
								<el-form-item label="栏目名称" prop="categoryName">
									<el-input v-model="form.categoryName" placeholder="请输入栏目名称" clearable></el-input>
								</el-form-item>
								<el-form-item label="描述" prop="categoryDescription">
									<el-input v-model="form.categoryDescription" placeholder="请输入描述" clearable></el-input>
								</el-form-item>
								<el-form-item label="编码" prop="categoryCode">
									<el-input v-model="form.categoryCode" placeholder="请输入编码" clearable></el-input>
								</el-form-item>
								<el-form-item label="关键字" prop="metaKeyword">
									<el-input v-model="form.metaKeyword" placeholder="请输入搜索引擎关键字" clearable></el-input>
								</el-form-item>
								<el-form-item label="会员级别" prop="membershipLevel">
									<el-input v-model="form.membershipLevel" placeholder="请输入会员级别 下拉框1--10" clearable></el-input>
								</el-form-item>
								<el-form-item label="发布区域" prop="regionPath">
									<el-input v-model="form.regionPath" placeholder="请输入发布区域" clearable></el-input>
								</el-form-item>
								<el-form-item label="模板路径" prop="templatePath">
									<el-input v-model="form.templatePath" placeholder="请输入模板路径" clearable></el-input>
								</el-form-item>
								<el-form-item label="标题图" prop="imageUrl">
									<el-input v-model="form.imageUrl" placeholder="请输入pc：图片url" clearable></el-input>
								</el-form-item>
								<el-form-item label="排序" prop="seq">
									<el-input v-model="form.seq" placeholder="请输入排序号" clearable></el-input>
								</el-form-item>
								<el-form-item label="状态" prop="status">
									<el-select v-model="form.status" placeholder="状态" clearable>
										<el-option label="激活" :value="1"></el-option><el-option label="失效" :value="0"></el-option>
									</el-select>
								</el-form-item>
								<el-form-item label="其它">
									<span>栏目路径:{{form.categoryPath}}</span>
									<span style="padding-left:20px;">产品数量:{{form.productCount}}</span>
								</el-form-item>
                            </el-form>
						</el-col>
					</el-row>
				</el-scrollbar>
			</el-main>
			<el-footer>
				<el-button v-if="mode!=='show'" :loading="isSaveing" @click="submit()" type="primary" size="small">保 存</el-button>
				<el-button size="small" @click="visible=false">取 消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>
<script>
	import productAPI from '@/api/product/category';
	export default {
		emits: ['success', 'closed'],
		props: {categoryTree: { type: Object, default: () => {} },},
		data() {
			return {
				mode: "add",
				systemTypeName: "",
				titleMap: {
					add: '新增栏目',
					edit: '编辑栏目',
					mode: '栏目'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					categoryId: "",
					sysBranchOfficeId: "",
					systemLevelId: "",
					categoryName: "",
					categoryDescription: "",
					categoryCode: "",
					productCount: "",
					metaKeyword: "",
					metaDescription: "",
					membershipLevel: "",
					regionPath: "",
					templatePath: "",
					categoryPath: "",
					imageUrl: "",
					pcFrontMenu: "",
					phFrontMenu: "",
					parentId: "",
					seq: null,
					status: null,
				},
				//验证规则
				rules: {
					categoryName: [{required: true, message: '请输入名称'}],
					categoryCode: [{required: true, message: '请输入名称'}],
					seq: [{pattern: /^[0-9]*$/, message: '排序需为数字', trigger: 'blur'}],
					price: [{pattern: /^\d+(\.\d+)?$/, message: '价格需为数字', trigger: 'blur'}],
				},
				superCategoryOptions: [],
				superCategoryProps: {
					value: 'categoryId',
					label: 'categoryName',
					multiple: false,//是否支持多选
					checkStrictly: true, //是否可以选择树干节点作为选项
					//lazy: true, // 是否动态加载子节点
					//lazyLoad: this.loadTreeNode,// lazyLoad加载动态数据的方法(仅在 lazy 为 true 时有效)
				},
			}
		},
		mounted() {
			this.superCategoryOptions = this.treeToMap(this.categoryTree);
		},
		watch: {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				//console.log('-------',this.form.parentId[0],this.form.parentId.length); 选择框值是数组
				if(this.form.parentId.length && this.form.parentId.length>0)
					this.form.parentId = this.form.parentId[0];
				this.$refs.dialogForm.validate(async (valid) => {
					if(valid){
						this.isSaveing = true;
						var res = await productAPI.category.save.post(this.form);
						this.isSaveing = false;
						if(res.code === 2000){
							this.$emit('success', this.form, this.mode);
							this.visible = false;
							this.$message.success(res.message);
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				});
			},
			//表单注入数据
			setData(data){
				this.form.categoryId = data.categoryId;
				this.form.sysBranchOfficeId = data.sysBranchOfficeId;
				this.form.systemLevelId = data.systemLevelId;
				this.form.categoryName = data.categoryName;
				this.form.categoryDescription = data.categoryDescription;
				this.form.categoryCode = data.categoryCode;
				this.form.productCount = data.productCount;
				this.form.metaKeyword = data.metaKeyword;
				this.form.metaDescription = data.metaDescription;
				this.form.membershipLevel = data.membershipLevel;
				this.form.regionPath = data.regionPath;
				this.form.templatePath = data.templatePath;
				this.form.categoryPath = data.categoryPath;
				this.form.imageUrl = data.imageUrl;
				this.form.pcFrontMenu = data.pcFrontMenu;
				this.form.phFrontMenu = data.phFrontMenu;
				this.form.parentId = data.parentId;
				this.form.seq = data.seq;
				this.form.status = data.status;
				this.systemTypeName = data.systemTypeName;
				this.titleMap.edit = data.systemTypeName+"--编辑栏目";
			},
			setDataAdd(systemLevelId,systemTypeName,sysBranchOfficeId){
				this.form.systemLevelId = systemLevelId;
				this.form.sysBranchOfficeId = sysBranchOfficeId;
				this.systemTypeName = systemTypeName;
				this.titleMap.add = systemTypeName+"--新增栏目";
				this.form.seq = 1;
				this.form.status = 1;
			},
			//简单化树结构
			treeToMap(tree){
				console.log('---------------',tree);
				if(tree && tree.length>0){
					const map = [];
					tree.forEach(item => {
						var obj = {
							categoryId: item.categoryId,
							parentId: item.parentId,
							categoryName: item.categoryName,
							children: item.children&&item.children.length>0 ? this.treeToMap(item.children) : null
						};
						map.push(obj);
					});
					return map;
				}
			},
		}
	}
</script>
<style>
</style>
